package webgamemaker.Common;

import java.util.Map;

public class AutoTile implements Tile 
{
	public Tile tiles[];
	
	public AutoTile(Tile tiles[])
	{
		this.tiles = tiles;
		if(tiles.length != 6 && tiles.length != 4 && tiles.length != 2)
			throw new RuntimeException("mauvais nombre de tiles");
	}
	
	public void draw(Map<String, Object> param) 
	{
		if(!param.containsKey("neighbour"))
			throw new RuntimeException("pas d'info sur les cases voisines");
		int size[] = tiles[0].getSize();
		if(!param.containsKey("x") || !param.containsKey("y"))
			throw new RuntimeException("position du tile non fourni");
		boolean hasWidth = param.containsKey("width");
		boolean hasHeight = param.containsKey("height");
		boolean hasSrcX = param.containsKey("srcX");
		boolean hasSrcY = param.containsKey("srcY");
		boolean hasSrcWidth = param.containsKey("srcWidth");
		boolean hasSrcHeight = param.containsKey("srcHeight");
		int x = (Integer)param.get("x");
		int y = (Integer)param.get("y");
		int width = size[0], height = size[1];
		if(hasWidth)
			width = (Integer)param.get("width");
		if(hasHeight)
			height = (Integer)param.get("height");
		int srcX = 0, srcY = 0;
		if(hasSrcX)
			srcX = (Integer)param.get("srcX");
		if(hasSrcY)
			srcY = (Integer)param.get("srcY");
		
		int srcWidth = width-srcX, srcHeight = height-srcY;
		if(hasSrcWidth)
			srcWidth = (Integer)param.get("srcWidth");
		if(hasSrcHeight)
			srcHeight = (Integer)param.get("srcHeight");
		boolean neighbour[] = (boolean[]) param.get("neighbour");
		if(tiles.length == 6)
		{		
			param.put("width", width/2);
			param.put("height", height/2);
			param.put("srcWidth", srcWidth/2);
			param.put("srcHeight", srcHeight/2);
			//NO
			if(neighbour[1] && neighbour[3])
			{
				if(neighbour[0])
					tiles[5].draw(param);
				else tiles[1].draw(param);
			}
			else if(neighbour[1])
				tiles[4].draw(param);
			else if(neighbour[3])
				tiles[3].draw(param);
			else tiles[2].draw(param);
			
			//NE
			param.put("srcX", srcX+srcWidth/2);
			param.put("x", x+width/2);
			if(neighbour[1] && neighbour[4])
			{
				if(neighbour[2])
					tiles[4].draw(param);
				else tiles[1].draw(param);
			}
			else if(neighbour[1])
				tiles[5].draw(param);
			else if(neighbour[4])
				tiles[2].draw(param);
			else tiles[3].draw(param);
			
			//SE
			param.put("srcY", srcY+srcHeight/2);
			param.put("y", y+height/2);
			if(neighbour[4] && neighbour[6])
			{
				if(neighbour[7])
					tiles[2].draw(param);
				else tiles[1].draw(param);
			}
			else if(neighbour[4])
				tiles[4].draw(param);
			else if(neighbour[6])
				tiles[3].draw(param);
			else tiles[5].draw(param);
			
			//SO
			param.put("srcX", srcX);
			param.put("x", x);
			if(neighbour[3] && neighbour[6])
			{
				if(neighbour[5])
					tiles[3].draw(param);
				else tiles[1].draw(param);
			}
			else if(neighbour[3])
				tiles[5].draw(param);
			else if(neighbour[6])
				tiles[2].draw(param);
			else tiles[4].draw(param);
		}
		else if(tiles.length == 4)
		{		
			param.put("width", width/2);
			param.put("height", height/2);
			param.put("srcWidth", srcWidth/2);
			param.put("srcHeight", srcHeight/2);
			//NO
			if(neighbour[1] && neighbour[3])
				tiles[3].draw(param);
			else if(neighbour[1])
				tiles[2].draw(param);
			else if(neighbour[3])
				tiles[1].draw(param);
			else tiles[0].draw(param);
			
			//NE
			param.put("srcX", srcX+srcWidth/2);
			param.put("x", x+width/2);
			if(neighbour[1] && neighbour[4])
				tiles[2].draw(param);
			else if(neighbour[1])
				tiles[3].draw(param);
			else if(neighbour[4])
				tiles[0].draw(param);
			else tiles[1].draw(param);
			
			//SE
			param.put("srcY", srcY+srcHeight/2);
			param.put("y", y+height/2);
			if(neighbour[4] && neighbour[6])
				tiles[0].draw(param);
			else if(neighbour[4])
				tiles[2].draw(param);
			else if(neighbour[6])
				tiles[1].draw(param);
			else tiles[3].draw(param);
			
			//SO
			param.put("srcX", srcX);
			param.put("x", x);
			if(neighbour[3] && neighbour[6])
				tiles[1].draw(param);
			else if(neighbour[3])
				tiles[3].draw(param);
			else if(neighbour[6])
				tiles[0].draw(param);
			else tiles[2].draw(param);
		}
		else if(tiles.length == 2)
		{
			param.put("width", width/2);
			param.put("srcWidth", srcWidth/2);
			if(neighbour[3])
				tiles[1].draw(param);
			else tiles[0].draw(param);
			
			param.put("srcX", srcX+srcWidth/2);
			param.put("x", x+width/2);
			
			if(neighbour[4])
				tiles[0].draw(param);
			else tiles[1].draw(param);
		}
		param.put("x", x);
		param.put("y", y);
		if(hasWidth)
			param.put("width", width);
		else param.remove("width");
		if(hasHeight)
			param.put("height", height);
		else param.remove("height");
		if(hasSrcX)
			param.put("srcX", srcX);
		else param.remove("srcX");
		if(hasSrcY)
			param.put("srcY", srcY);
		else param.remove("srcY");
		if(hasSrcWidth)
			param.put("srcWidth", srcWidth);
		else param.remove("srcWidth");
		if(hasSrcHeight)
			param.put("srcHeight", srcHeight);
		else param.remove("srcHeight");
	}

	public int[] getSize() 
	{
		return tiles[0].getSize();
	}

}
